package net.dopan.pigframe.core.swagger;

import io.swagger.annotations.Api;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package: net.dopan.pigframe.core.swagger
 * @Title: BaseSwagger2Config
 * @Description: swagger 配置
 * @author: 刘宽
 * @date: 2018/9/2 17:39
 */
public abstract class BaseSwagger2Config {

    protected Docket createRestApi(String applicationName, String version) {
        ParameterBuilder tokenBuilder = new ParameterBuilder();
        List<Parameter> parameterList = new ArrayList<>();
        tokenBuilder.name("Authorization")
                .defaultValue("去其他请求中获取heard中token参数")
                .description("令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(true).build();
        parameterList.add(tokenBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(setHeaderAuth())
                .apiInfo(apiInfo(applicationName, version))
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.basePackage("net.dopan.pigframe"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameterList);
    }

    protected List<Parameter> setHeaderAuth() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization").description("access_token (example: bearer XXX)")
                .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }

    protected Contact getContact() {
        return new Contact("pigframe", "www.ilkhome.cn", "250502876@qq.com");
    }

    protected ApiInfo apiInfo(String applicationName,String version) {
        return new ApiInfoBuilder()
                .title(applicationName + "接口")
                .description(applicationName + "接口说明文档")
                //.termsOfServiceUrl("www.ilkhome.cn")
                .contact(getContact())
                .version(version)
                .build();
    }

}